//
//  UIString-Extension.swift
//  Chihiro
//
//  Created by point on 2016/11/9.
//  Copyright © 2016年 chihiro. All rights reserved.
//
import Foundation

extension String  {
    static func getIntFromString(str:String) -> String {
        let scanner = Scanner(string: str)
        scanner.scanUpToCharacters(from: CharacterSet.decimalDigits, into: nil)
        var number :Int = 0
        scanner.scanInt(&number)
        if number > 10000 { number = 813}
        return String(number)
    }
    
    //时间转化1
    static func timeToString(time:Int) -> String {
        let string = NSString(string: String(time))
        let timeSta:TimeInterval = string.doubleValue
        let dfmatter = DateFormatter()
        dfmatter.dateFormat="yyyy-MM-dd"
        let date = NSDate(timeIntervalSince1970: timeSta)
        print(dfmatter.string(from: date as Date))
        return dfmatter.string(from: date as Date)
    }
    
    static func showApiCurrentTime() ->  String {
        let date = NSDate()
        let timeFormatter = DateFormatter()
        timeFormatter.dateFormat = "yyyyMMddHHmmss"
        return timeFormatter.string(from: date as Date) as String
    }
    
    static func timeStringToDate(timeStr:String) -> Date? {
        let timeFormatter = DateFormatter()
        timeFormatter.dateFormat = "yyyyMMddHHmmss"
        guard let date = timeFormatter.date(from: timeStr) else {return nil}
        return date
    }
    
    static func timeStampToTimeStr(timeStamp:String) -> Date? {
        
        guard let timeInterval = Int64(timeStamp) else {return nil}
        return Date(timeIntervalSince1970: TimeInterval(timeInterval))
    }
    
    
    static func stringToTimeStamp(stringTime:String)->String {
        if stringTime.count == 0 {return "时间格式有误"}
        let time:TimeInterval =  TimeInterval(Int64(stringTime)! / 1000)
        let dfmatter1 = DateFormatter()
        dfmatter1.dateFormat="yyyy-MM-dd HH:mm       "
        let date1 = NSDate(timeIntervalSince1970: time)
        return dfmatter1.string(from: date1 as Date)
    }
    
    
    //MARK: 将时间显示为（几分钟前，几小时前，几天前）
    static func timeStringToShow(str:String) -> String {
        
        guard let timeDate = String.timeStampToTimeStr(timeStamp: str) else { return "未知时间" }
        
        let currentDate = NSDate()
        
        let timeInterval = currentDate.timeIntervalSince(timeDate)
        var temp:Double = 0
        var result:String = ""
        if timeInterval/60 < 1 {
            result = "刚刚"
        }else if (timeInterval/60) < 60{
            temp = timeInterval/60
            result = "\(Int(temp))分钟前"
        }else if timeInterval/60/60 < 24 {
            temp = timeInterval/60/60
            result = "\(Int(temp))小时前"
        }else if timeInterval/(24 * 60 * 60) < 30 {
            temp = timeInterval / (24 * 60 * 60)
            result = "\(Int(temp))天前"
        }else if timeInterval/(30 * 24 * 60 * 60)  < 12 {
            temp = timeInterval/(30 * 24 * 60 * 60)
            result = "\(Int(temp))个月前"
        }else{
            temp = timeInterval/(12 * 30 * 24 * 60 * 60)
            result = "\(Int(temp))年前"
        }
        return result
    }

    
    
    func validateTel ()-> Bool {
        
        let regex = "^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$"
        
        let rest:NSPredicate = NSPredicate(format: "SELF MATCHES %@", regex)
        
        return rest.evaluate(with: self)
        
    }
    
    
    func currentTime() ->String {
        let now = Date()
        let timeInterval:TimeInterval = now.timeIntervalSince1970
        let timeStamp = Int(timeInterval)
        return "\(timeStamp)"
    }
    
    
//    //md5
//    var md5: String! {
//        let str = self.cString(using: String.Encoding.utf8)
//        let strLen = CC_LONG(self.lengthOfBytes(using: String.Encoding.utf8))
//        let digestLen = Int(CC_MD5_DIGEST_LENGTH)
//        let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
//
//        CC_MD5(str!, strLen, result)
//
//        let hash = NSMutableString()
//        for i in 0..<digestLen {
//            hash.appendFormat("%02x", result[i])
//        }
//        result.deallocate(capacity: digestLen)
//        return String(format: hash as String)
//    }
    
    
    func apiFormat() ->String {
        let now = Date()
        let timeInterval:TimeInterval = now.timeIntervalSince1970
        let timeStamp = String(Int(timeInterval))
        
        let a = "%E6%97%B6%E9%97%B4%E6%88%B3";
        let b = "%E7%AD%BE%E5%90%8D%2C1%3A%E6%97%B6%E9%97%B4%E6%88%B3%E6%9C%80%E5%90%8E%E4%B8%80%E4%BD%8D%E6%95%B0%E5%AD%97%E4%B8%BA%E5%A5%87%E6%95%B0sign%E5%8A%A0%E5%AF%86%E6%96%B9%E5%BC%8F%E4%B8%BAmd5%EF%BC%88id%2Bremark%2Baddress%2Blatitude%2BappointmentNum%2Bphone%2BuserId%2Btonken%EF%BC%892%3A%E6%97%B6%E9%97%B4%E6%88%B3%E6%9C%80%E5%90%8E%E4%B8%80%E4%BD%8D%E6%95%B0%E5%AD%97%E4%B8%BA%E5%81%B6%E6%95%B0sign%E5%8A%A0%E5%AF%86%E6%96%B9%E5%BC%8F%E4%B8%BAmd5%EF%BC%88name%2Bpreferential%2Blongitude%2Binfo%2Bscore%2BisOpen%2Btimestamp%2Btonken%EF%BC%89"
        
        let newString = self + "?timestamp=" + a + "&sign=" + b
        
        return newString
        
    }
    
    
    func apiF(time:String ,sign:String) ->String {
        let newString = self + "?timestamp=" + time + "&sign=" + sign + "&operatorId=" + String(UserInfo.shareInstance.userModel().id) + "&userId=" + String(UserInfo.shareInstance.userModel().id)
        return newString
    }
    
    func apiFOperator(time:String ,sign:String) ->String {
        let newString = self + "?timestamp=" + time + "&sign=" + sign + "&operatorId=" + String(UserInfo.shareInstance.userModel().id)
        return newString
    }
    
}

